//
//  LSMainController.swift
//  LSWeiBo0808
//
//  Created by 李明禄 on 2017/8/10.
//  Copyright © 2017年 SocererGroup. All rights reserved.
//

import UIKit

class LSMainController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
//        self.view.backgroundColor = UIColor.red
        // Do any additional setup after loading the view.
        
        setController()
        setCompose()
    }

    lazy var Compose: UIButton = UIButton.cz_imageButton("tabbar_compose_icon_add", backgroundImageName: "tabbar_compose_button")
    
    
    
    }

extension LSMainController {
    
    fileprivate func setCompose() {
        
 
        
        let ComposeWidth = tabBar.bounds.width / CGFloat(self.childViewControllers.count)
        let ComposeHeight = tabBar.bounds.height
        
        let composeX = 2 * ComposeWidth

        
        Compose.frame = CGRect(x: composeX, y: 0, width: ComposeWidth, height: ComposeHeight)
            //        self.view.addSubview(Compose)
//        Compose.center = CGPoint(x: tabBar.yw_centerX, y: tabBar.yw_centerY)
        

        tabBar.addSubview(Compose)
        
        Compose.addTarget(nil, action: #selector(addEvent), for: .touchUpInside)
    }
    
    @objc private func addEvent() {
        print("你点击了+按钮")
    }
    
    fileprivate func setController() {
        let array = [
            ["clsName": "LSHomeViewController", "title": "首页", "imageName": "home"],
            ["clsName": "LSHomeViewController", "title": "消息", "imageName": "message_center"],
            ["clsName": "Plus"],
            ["clsName": "LSHomeViewController", "title": "发现", "imageName": "discover"],
            ["clsName": "LSHomeViewController", "title": "我的", "imageName": "profile"]
        ]
        
        var controllers = [UIViewController]()
        
        for dict in array {
            controllers.append(controller(dict: dict))
        }
        
        self.viewControllers = controllers
    }
    
    private func controller(dict: [String: String]) -> UIViewController {
        guard let clsName = dict["clsName"],
            let title = dict["title"],
            let imageName = dict["imageName"],
            let cls = NSClassFromString(Bundle.main.nameSpace + "." + clsName) as? UIViewController.Type
                else {
                    return UIViewController()
                }
        let VC = cls.init()
        
        VC.title = title
        VC.tabBarItem.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFont(ofSize: 14)], for: UIControlState.init(rawValue: 0))
        VC.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orange], for: UIControlState.highlighted)
        
        VC.tabBarItem.image = UIImage(named: "tabbar_" + imageName)
        VC.tabBarItem.selectedImage = UIImage(named: "tabbar_" + imageName + "_highlighted")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        
        let nav = LSNavViewController(rootViewController: VC)
        
        return nav
        
    }
}


